package fu.capstone.hanoicityguide.presentation.activity;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.R.layout;
import fu.capstone.hanoicityguide.presentation.adapter.TabListener;
import fu.capstone.hanoicityguide.presentation.fragment.home.ListPlaceFragment;
import fu.capstone.hanoicityguide.presentation.fragment.transportation.FragmentBusTab;
import fu.capstone.hanoicityguide.presentation.fragment.transportation.FragmentCycloTab;
import fu.capstone.hanoicityguide.presentation.fragment.transportation.FragmentTaxiTab;
import fu.capstone.hanoicityguide.presentation.fragment.transportation.ListTransportFragment;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.SearchView;
import android.widget.Toast;

public class TransportationActivity extends Activity implements
		FragmentManager.OnBackStackChangedListener {

	private ActionBar.Tab taxiTab, busTab, cycloTab;
	private Fragment fragmentTaxiTab;
	private Fragment fragmentBusTab;
	private Fragment fragmentCycloTab;
	private Menu menu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transportation_fragment);
		fragmentTaxiTab = new FragmentTaxiTab();
		fragmentBusTab = new FragmentBusTab();
		fragmentCycloTab = new FragmentCycloTab();
		getView();
		ActionBar actionBar = getActionBar();
		// set icon and title of the tab
		taxiTab = actionBar.newTab().setText("Taxi");
		busTab = actionBar.newTab().setText("Bus");
		cycloTab = actionBar.newTab().setText("Cyclo");

		// Set Tab Listeners
		taxiTab.setTabListener(new TabListener(fragmentTaxiTab));
		busTab.setTabListener(new TabListener(fragmentBusTab));
		cycloTab.setTabListener(new TabListener(fragmentCycloTab));

		// Add tabs to actionbar
		actionBar.addTab(taxiTab);
		actionBar.addTab(busTab);
		actionBar.addTab(cycloTab);
		getFragmentManager().addOnBackStackChangedListener(this);
	}

	public void getView() {

		ActionBar actionBar = getActionBar();

		getActionBar().setTitle("Transportation");
		getActionBar().setBackgroundDrawable(
				new ColorDrawable(Color.parseColor("#0099CC")));
		getActionBar().setDisplayHomeAsUpEnabled(true);
		// getActionBar().setDisplayShowHomeEnabled(false);

		// create action bar tab mode
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// create option menu in actionbar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.list_transport_menu, menu);
		this.menu = menu;
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// search all in home fragment
		switch (item.getItemId()) {
		case R.id.action_search_transport:
			// create action bar tab mode
			getActionBar()
					.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
			Fragment listTransport = new ListTransportFragment();
			FragmentManager fragmentManager = getFragmentManager();
			FragmentTransaction fTransaction = fragmentManager
					.beginTransaction();
			fTransaction.replace(R.id.container, listTransport);
			fTransaction.addToBackStack("SearchTransport");
			fTransaction.commit();
			this.setTitle("Search Transportation");

			break;

		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackStackChanged() {
		// TODO Auto-generated method stub
		FragmentManager fManager = getFragmentManager();
		FragmentTransaction fTransaction = fManager.beginTransaction();

		if (fManager.getBackStackEntryCount() == 1) {
			menu.getItem(0).setVisible(false);
		}
		if (fManager.getBackStackEntryCount() == 0) {
			
			getView();
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

}
